import Vue from 'vue'
import Router from 'vue-router'
import my from './user'
import discover from './discover'
import goods from './goods'
import packageRouter from './package'
import login from './login'
import home from './home'
import category from './category'
import shopcar from './shopcar'
import seller from './seller'
import message from './message'
import coupon from './coupon'
import transaction from './transaction'
import map from './map'
import cartype from './cartype'
import rentcar from './rentcar'
import usedcar from './usedcar'
import transfer from './transfer'
import recruit from './recruit'
import job from './job'
import shopscenter from './shopscenter'
// import backstage from './backstage'
import $store from '@store'

Vue.use(Router)

const search = () => import('@views/search')

const router = new Router({
  // base: '/mobile', // history模式使用， hash模式去除
  // mode: 'history', // history模式使用， hash模式去除
  routes: [
    ...my,
    ...discover,
    ...goods,
    ...packageRouter,
    ...login,
    ...home,
    ...category,
    ...shopcar,
    ...seller,
    ...recruit,
    ...message,
    ...coupon,
    ...map,
    ...transaction,
    ...cartype,
    ...rentcar,
    ...usedcar,
    ...transfer,
    ...job,
    ...shopscenter,
    // ...backstage,
    {
      path: '/search',
      component: search,
      meta: {
        title: '搜索'
      }
    },
    {
      path: '/',
      redirect: '/home'
    }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = '赞佳乐'
  let toPath = to.path
  // 首次打开显示启动广告
  let firstLoad = sessionStorage.getItem('firstLoad')
  let token = $store.state.token

  if (firstLoad && toPath === '/launch') {
    next('/')
    return false
  } else if (!token && to.meta.isLogin === true) {
    console.log('to.fullPath => ' + to.fullPath)
    next({
      path: '/login',
      query: {redirect: to.fullPath}
    })
  } else {
    next()
  }
})

export default router
